﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NDepot.Repository;
using NDepot.Models;
using System.Reflection;
using NLog;
using NDepot.Models.UI;
using NDepot.Repository.Base;
using NDepot.Manager.Base;
using NDepot.Manager;
using NDepot.Extensions;
using NDepotControllers.Filters;

namespace NDepot.Controllers
{
    public class StoreController : DepotController
    {
        private IProductManager _ProductManager;
        private IOrderManager _OrderManager;

        public StoreController()
            : this(ProductManager.GetInstance(), OrderManager.GetInstance())
        {
        }

        public StoreController(IProductManager product_r, IOrderManager order_r)
        {
            _ProductManager = product_r;
            _OrderManager = order_r;
        }

        const string CART_KEY = "shopping_cart";

        //
        // GET: /Store/

        public ActionResult Index()
        {
            CartInput cartView = new CartInput();
            cartView.Cart = FindCart();
            cartView.Products = _ProductManager.FindProductsForSale();
            return View(cartView);
        }

        public ActionResult AddToCart(string id)
        {
            try
            {
                ProductInput product = _ProductManager.GetInputById(id);
                Cart cart = FindCart();
                cart.AddProduct(product);

                if (Request.IsAjaxRequest())
                    return View("Cart", cart);
                else
                    return RedirectToIndex(null);
            }
            catch
            {
                logger.Error("Attempt to access invalid product #" + id);
                return RedirectToIndex("Invalid product.");
            }
        }

        public ActionResult EmptyCart()
        {
            Session[CART_KEY] = null;
            return RedirectToIndex("Your cart is currently empty");
        }

        [ImportModelStateFromTempData]
        public ActionResult CheckOut()
        {
            Cart cart = FindCart();
            if (cart.Items.Count == 0)
                return RedirectToIndex("Your cart is empty");
            else {
                var order = new OrderInput();
                ViewData["PaymentType"] = new SelectListItem[] {
                        new SelectListItem { Text = "Cash", Value = "cash" },
                        new SelectListItem { Text = "Credit Card", Value = "cc" },
                        new SelectListItem { Text = "Purchase Order", Value = "po" },
                    };
                return View(order);
            }
        }

        [HttpPost, ExportModelStateToTempData]
        public ActionResult SaveOrder(OrderInput oi)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Cart cart = FindCart();

                    _OrderManager.Insert(oi, cart.Items.Values);
                    Session[CART_KEY] = null;
                    return RedirectToIndex("Thank you for your order");
                }
            }
            catch (Exception e)
            {
                logger.Error(e.Message);
            }
            return RedirectToAction("CheckOut");
        }

        #region Private Method

        private Cart FindCart()
        {
            if (Session[CART_KEY] == null)
                Session.Add(CART_KEY, new Cart());

            return (Cart)Session[CART_KEY];
        }

        private ActionResult RedirectToIndex(string message)
        {
            TempData["Notice"] = message;
            return Redirect(Url.Home());//RedirectToAction("Index");
        }

        #endregion
    }
}
